use Test::More tests => 6;

use strict;
use warnings;

use Data::Dumper;

use Thrift::BinaryProtocol;
use Thrift::BufferedTransport;
use Thrift::Socket;

use Hbase::Hbase;


my $sock = Thrift::Socket->new('localhost', '9090');
$sock->setRecvTimeout(10000);
my $transport = Thrift::BufferedTransport->new($sock);
my $protocol = Thrift::BinaryProtocol->new($transport);
my $client = Hbase::HbaseClient->new($protocol);

$transport->open();

my $test_table = 'test_table';


eval {
    $client->disableTable($test_table);
    $client->deleteTable($test_table);
};


{
    my $columns = [
        Hbase::ColumnDescriptor->new({name => 'foo:',}),
        Hbase::ColumnDescriptor->new({name => 'bar:',}),
    ];
    $client->createTable($test_table, $columns);
    my %tables = map { $_ => 1 } @{$client->getTableNames()};
    ok($tables{$test_table});

    eval {
        $client->createTable($test_table, $columns);
    };
    is($@->isa('Hbase::AlreadyExists'), 1);

    ok($client->isTableEnabled($test_table));
    eval {
        $client->deleteTable($test_table);
    };
    is($@->isa('Hbase::IOError'), 1);

    $client->disableTable($test_table);
    ok(!$client->isTableEnabled($test_table));
    $client->deleteTable($test_table);
    %tables = map { $_ => 1 } @{$client->getTableNames()};
    ok(!$tables{$test_table});
}

$transport->close();
